//
//  TableViewCell.m
//  learn_async
//
//  Created by liboxiang on 2019/2/27.
//  Copyright © 2019 liboxiang. All rights reserved.
//

#import "TableViewCell.h"
#import "NSString+BTExtend.h"
#import "UILabel+Async.h"

extern int LabelNumbet;
@interface TableViewCell()
@property (strong, nonatomic) NSMutableArray *labelsArray;
@end

@implementation TableViewCell
+ (NSString *)identifier
{
    return  NSStringFromClass([self class]);
}
+ (CGFloat)cellHeight
{
    return 30*LabelNumbet/2;
}

+ (CGFloat)cellHeigtForDic:(NSDictionary *)dic {
    CGFloat width = [UIScreen mainScreen].bounds.size.width / 2;
    CGFloat height = 0;
    for (int i = 0; i < LabelNumbet / 2; i++) {
        CellModel *modelLeft = dic[[NSString stringWithFormat:@"%d",i * 2]];
        CellModel *modelRight = dic[[NSString stringWithFormat:@"%d",i * 2 + 1]];
        modelLeft.height = [TableViewCell heightForStr:modelLeft.str font:[UIFont systemFontOfSize:modelLeft.font] width:width];
        modelRight.height = [TableViewCell heightForStr:modelRight.str font:[UIFont systemFontOfSize:modelRight.font] width:width];
        height += modelLeft.height > modelRight.height ? modelLeft.height : modelRight.height;
        
    }
    return height;
}

+ (CGFloat)heightForStr:(NSAttributedString *)str font:(UIFont *)font width:(CGFloat)width{
    NSMutableDictionary *attr = [NSMutableDictionary new];
    attr[NSFontAttributeName] = font;
    CGRect rect = [str boundingRectWithSize:CGSizeMake(width, HUGE) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    return rect.size.height + 1;
}

+ (void)registerForTable:(UITableView *)tableView
{
    [tableView registerClass:[self class] forCellReuseIdentifier:[self identifier]];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    [self setupUI];
    return self;
}

- (void)setupUI {
    _labelsArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < LabelNumbet; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 * (i % 2), (i / 2) * 30, [UIScreen mainScreen].bounds.size.width / 2, 30)];
        label.numberOfLines = 0;
        [self.contentView addSubview:label];
        [_labelsArray addObject:label];
    }
}

- (void)refreshWithDic:(NSDictionary *)dic {
    CGFloat originY = 0;
    CGFloat leftHeight = 0;
    for (int i = 0; i < LabelNumbet; i++) {
        CellModel *model = dic[[NSString stringWithFormat:@"%d",i]];
        UILabel *label = _labelsArray[i];
        label.font = [UIFont systemFontOfSize:model.font];
        label.attributedText = model.str;
        CGRect frame = label.frame;
        frame.size.height = model.height;
        frame.origin.y = originY;
        label.frame = frame;
//        NSLog(@"%@",NSStringFromCGRect(frame));
        if (i % 2 == 0) {
            leftHeight = model.height;
        }
        else {
            originY += MAX(leftHeight, model.height);
        }
    }
}

- (void)asyncRefreshWithDic:(NSDictionary *)dic {
    CGFloat originY = 0;
    CGFloat leftHeight = 0;
    for (int i = 0; i < LabelNumbet; i++) {
        CellModel *model = dic[[NSString stringWithFormat:@"%d",i]];
        UILabel *label = _labelsArray[i];
        label.font = [UIFont systemFontOfSize:model.font];
        CGRect frame = label.frame;
        frame.size.height = model.height;
        frame.origin.y = originY;
        label.frame = frame;
//        label.asyncText = model.str.string;
        label.asyncAttributedText = model.str;
//        NSLog(@"%@",NSStringFromCGRect(frame));
        if (i % 2 == 0) {
            leftHeight = model.height;
        }
        else {
            originY += MAX(leftHeight, model.height);
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
